package msr.validator;

import msr.object.LoginUser;
import msr.service.UserService;
import msr.util.Message;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

public class UserValidator extends ExpertValidator {

	public void validate(Object tagets, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username",
				"required.Username", Message.LOST_USERNAME);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwd",
				"required.Passwd", Message.LOST_PASSWD);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "currpasswd",
				"required.CurrPasswd", Message.LOST_CURRPASSWD);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newpasswd",
				"required.NewPasswd", Message.LOST_NEWPASSWD);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmpasswd",
				"required.ConfirmPasswd", Message.LOST_CONFIRMPASSWD);
		UserService uService = new UserService();
		LoginUser u = (LoginUser) tagets;
		if (!uService.checkLogin(u.getUsername(), u.getPasswd()))
			errors.rejectValue("login", "notmatch.login");
		if (uService.checkEqualPassword(u.getPasswd(), u.getNewpasswd()))
			errors.rejectValue("passwd", "conflict.passwds");
		if (!uService
				.checkEqualPassword(u.getNewpasswd(), u.getConfirmpasswd()))
			errors.rejectValue("passwd", "notmatch.passwd");
	}
}